import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_nft/provider/LoginProvider.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LogingPage extends StatefulWidget {
  @override
  _LogingPageState createState() => _LogingPageState();
}

class _LogingPageState extends State<LogingPage> {

  String token = "";
  String phone = "";

  @override
  void initState() {
    getShared();
    //获取缓存数据
    Timer(Duration(seconds: 0),(){
        Navigator.of(context).pushReplacementNamed("tabbar"); //tabbar
    });
    // TODO: implement initState
    super.initState();

  }

  getShared()async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? resultPhone = prefs.get("phone") as String?;
    bool? isLogin = prefs.getBool("isLogin");
    //String? resultToken = prefs.get("token") as String?;
    //写入状态
    context.read<LoginProvider>().setUserName(resultPhone.toString());
    context.read<LoginProvider>().setIsLogin(isLogin!);
    if(resultPhone != null){
      setState(() {
        //token = resultToken;
        phone = resultPhone;
      });
    }

  }

  @override
  Widget build(BuildContext context) {
    //存入状态
    context.read<LoginProvider>().setToken(token.toString());
    context.read<LoginProvider>().setUserName(phone.toString());
    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(image: AssetImage("lib/assets/loding.jpg"),fit: BoxFit.fill)),
    );
  }
}
